package com.ping.controller;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.ping.model.ResponseEnum;

public class BaseController extends Controller{
	//About HttpServletRequest
	public static final String QUERY_STATEMENT="query.statement";
	public static final String REQUEST = "request";
	public static final String RESULT = "result";
	public static final String TOKEN = "token";
	
	//About HttpServletResponse
	public static final String STATUSCODE = "statusCode";
	
	private static final String LEFT = "(";
	private static final String RIGHT = ")";
	
 	protected String callback;
	
	public boolean getCallback(){
		if( getPara("calback") == null ){
			return false;
		}
		this.callback = getPara("callback");
		return true;
	}
	
	public boolean checkQuery(){
		if ( getPara(QUERY_STATEMENT) == null ){
			renderStatus(ResponseEnum.BASESERVICE_ERROR1.getKey(),
					ResponseEnum.BASESERVICE_ERROR1.getValue(),
					ResponseEnum.BASESERVICE_ERROR1.getDesc());
			return false;
		}
		return true;
	}
	
	public void putToken(JSONObject result) throws JSONException{
		result.put(TOKEN, genToken());
	}
	
	public String genToken(){
		return "token";
	}
	
	public void putRecord(JSONObject result, List<Record> records) throws JSONException{
		JSONArray array = new JSONArray();
		for (Record record : records) {
			JSONObject object = new JSONObject();
			for (int i = 0; i < record.getcolumnNames().length; i++) {
				object.put(record.getcolumnNames()[i], 
						record.getcolumnValues()[i]);
			}
			array.put(object);
		}
		result.put(RESULT, array);
	}
	
	public void putStatus(JSONObject object, String key, int value, String desc) throws JSONException{
		object.put(STATUSCODE, value);
		object.put(key, desc);
	} 
	
	public void renderStatus(String key, int value, String desc){
		JSONObject error = new JSONObject();
		try {
			putStatus(error, key, value, desc);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pingRender(error);
		return;
	}
	
	public void renderPingError(){
		renderStatus(ResponseEnum.PINGSERVICE_ERROR1.getKey(),
				ResponseEnum.PINGSERVICE_ERROR1.getValue(), 
				ResponseEnum.PINGSERVICE_ERROR1.getDesc());
	}
	
	public void renderSuccess(){
		renderStatus(ResponseEnum.SUCCESSFUL.getKey(),
				ResponseEnum.SUCCESSFUL.getValue(), 
				ResponseEnum.SUCCESSFUL.getDesc());
	}
	
	public void renderOperateError(){
		renderStatus(ResponseEnum.BASESERVICE_ERROR2.getKey(), 
				ResponseEnum.BASESERVICE_ERROR2.getValue(),
				ResponseEnum.BASESERVICE_ERROR2.getDesc());
	}
	
	public void renderLack(){
		renderStatus(ResponseEnum.LACK_NACESSARY_PARAMATERS.getKey(), 
				ResponseEnum.LACK_NACESSARY_PARAMATERS.getValue(), 
				ResponseEnum.LACK_NACESSARY_PARAMATERS.getDesc());
	}
	
	public void renderNullResult(){
		renderStatus(ResponseEnum.USERSERVICE_ERROR1.getKey(), 
				ResponseEnum.USERSERVICE_ERROR1.getValue(), 
				ResponseEnum.USERSERVICE_ERROR1.getDesc());
	}
	
	public void renderResult(List<Record> records, boolean hasToken){
		JSONObject result = new JSONObject();
		
		try {
			if (records.size() == 0) {
				putStatus(result, ResponseEnum.NULL_RESULTSET.getKey(),
						ResponseEnum.NULL_RESULTSET.getValue(), 
						ResponseEnum.NULL_RESULTSET.getDesc());
			}else {
				putStatus(result, ResponseEnum.SUCCESSFUL.getKey(),
						ResponseEnum.SUCCESSFUL.getValue(), 
						ResponseEnum.SUCCESSFUL.getDesc());
				
				putRecord(result, records);
			}
			
			if (!hasToken) {
				putToken(result);
			}
			
			pingRender(result);
			return;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			System.out.println(ResponseEnum.JSON_PUT_ERROR.getDesc());
			return;
		}
	}
	
	public void renderJsonp(JSONObject object){
		StringBuffer result = new StringBuffer(this.callback);
		renderText(result.append(LEFT)
				.append(object.toString())
				.append(RIGHT).toString()
				);
	}
	
	public void renderJson(JSONObject object){
		renderText(object.toString());
	}
	
	public void pingRender(JSONObject object){
		if (getCallback()) {
			renderJsonp(object);
		}else {
			renderJson(object);
		}
		return;
	}
}
